package number_19;

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode curr = dummy, prev = dummy;
        while (n-- > 0) {
            curr = curr.next;
        }

        while (curr.next != null) {
            prev = prev.next;
            curr = curr.next;
        }
        ListNode tmpNode = prev.next;
        prev.next = tmpNode.next;
        return dummy.next;

    }

    class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }
}
